<?php

use yii\helpers\Html;
use common\components\GridView;
use yii\widgets\Pjax;
use yii\helpers\Url;
use kartik\editable\Editable;
use backend\models\system\Config;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\system\search\ConfigSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

?>

<?php Pjax::begin(['id'=>'grid-pjax']); ?>
<div class="config-index">

    <?php  echo $this->render('_search', ['model' => $searchModel]); ?>

    <?= GridView::widget([
        'id' => 'grid-id',
        'dataProvider' => $dataProvider,
//        'filterModel' => $searchModel,
//        'showPageSummary' => true,
        'pjax' => true,
        'striped' => true,
        'hover' => true,
        'toolbar' => [
            '{export}',
            ['content' =>
                Html::a('<i class ="glyphicon glyphicon-repeat"></i>', ['index'], ['data-pjax' => 0, 'class' => 'btn btn-default', 'title' => 'Reset Grid'])
            ],
            'options' => ['class' => 'btn-group-sm']
        ],
        'exportConfig' => [
            GridView::CSV => ['label' => 'CSV', 'icon' => 'floppy-open'],
        ],
        'panel' => [
            'heading' => '<i class="glyphicon glyphicon-th-list"></i>',
            'type' => 'default',
            'before' => Html::a('<i class="glyphicon glyphicon-plus"></i> 添加', ['create'], ['class' => 'btn btn-primary btn-sm']) . ' ' .
                Html::a('<i class="glyphicon glyphicon-edit"></i> 编辑', 'javascript:void(0);', ['class' => 'btn btn-success btn-sm btn-update']) . ' ' .
                Html::a('<i class="glyphicon glyphicon-remove"></i> 删除', 'javascript:void(0);', ['class' => 'btn btn-danger btn-sm batch-delete']),
        ],
        'pager' => [
            'firstPageLabel' => '首页',
            'lastPageLabel' => '尾页',
            'registerLinkTags' => true,
            'disableCurrentPageButton' => true,
            'searchPage' => true,
            'searchPageLabel' => '跳至',
            'pageCount' => true,
            'pageSize' => true,
        ],
        'columns' => [
            [
                'class' => \common\core\CheckboxColumn::className(),
                'name'  => 'id',
                'options' => ['width' => '20px;'],
                'checkboxOptions' => function ($model, $key, $index, $column) {
                    return ['value' => $key,'label'=>'<span></span>','labelOptions'=>['class' =>'mt-checkbox mt-checkbox-outline','style'=>'padding-left:19px;']];
                }
            ],
            [
                'class' => 'kartik\grid\SerialColumn',
                'pageSummary' => '总计:',
                'pageSummaryOptions' => ['class' => 'text-right text-warning'],
            ],
            [
                'class' => 'kartik\grid\EditableColumn',
                'attribute' => 'name',
                'editableOptions' => function ($model, $key, $index) {
                    return [
                        'model' => $model,
                        'attribute' => 'name',
                        'options' => [
                            'id' => 'Config-' . "{$index}". 'name',
                            'name' => 'Config[' . "{$index}" . '][name]',
                            'style' => 'width:auto;',
                            'title' => $model->name,
                        ],
                        'formOptions' => ['action' => ['edit-table']],
                        'buttonsTemplate' => '{submit}',
                        'inlineSettings' => [
                            'closeButton' => '<button class="btn btn-sm btn-danger kv-editable-close"><i class="glyphicon glyphicon-remove"></i></button>',
                        ],
                        'asPopover' => true,
                        'placement' => 'right',
                    ];
                },
                'hAlign' => 'left',
                'vAlign' => 'middle',
                'width' => '20%',
            ],
            [
                'class' => 'kartik\grid\EditableColumn',
                'attribute' => 'title',
                'editableOptions' => function ($model, $key, $index) {
                    return [
                        'model' => $model,
                        'attribute' => 'title',
                        'options' => [
                            'id' => 'Config-' . "{$index}" . 'title',
                            'name' => 'Config[' . "{$index}" . '][title]',
                            'style' => 'width:auto;',
                            'title' => $model->title,
                        ],
                        'formOptions' => ['action' => ['edit-table']],
                        'buttonsTemplate' => '{submit}',
                        'inlineSettings' => [
                            'closeButton' => '<button class="btn btn-sm btn-danger kv-editable-close"><i class="glyphicon glyphicon-remove"></i></button>',
                        ],
                        'asPopover' => true,
                        'placement' => 'right',
                    ];
                },
                'hAlign' => 'left',
                'vAlign' => 'middle',
                'width' => '30%',
            ],
            [
                'class' => 'kartik\grid\EditableColumn',
                'attribute' => 'type',
                'label' => '类型',
                'editableOptions' => function ($model, $key, $index) {
                    return [
                        'model' => $model,
                        'value' => $model->type,
                        'data' => Config::getType(),
                        'displayValueConfig' => Config::getType(),
                        'asPopover' => true,
                        'formOptions' => ['action' => ['edit-table']],
                        'inputType' => Editable::INPUT_RADIO_LIST,       //  INPUT_RADIO_LIST  INPUT_DROPDOWN_LIST
                        'buttonsTemplate' => '{submit}',
                        'inlineSettings' => [
                            'closeButton' => '<button class="btn btn-sm btn-danger kv-editable-close"><i class="glyphicon glyphicon-remove"></i></button>',
                        ],
                        'placement' => 'left',
                        'options' => ['style' => 'height:60px'],
                    ];
                },
                'hAlign' => 'center',
                'vAlign' => 'middle',
                'width' => '10%'
            ],
            [
                'class' => 'kartik\grid\EditableColumn',
                'attribute' => 'group',
                'label' => '分组',
                'editableOptions' => function ($model, $key, $index) {
                    return [
                        'model' => $model,
                        'value' => $model->group,
                        'data' => Config::getGroup(),
                        'displayValueConfig' => Config::getGroup(),
                        'asPopover' => true,
                        'formOptions' => ['action' => ['edit-table']],
                        'inputType' => Editable::INPUT_RADIO_LIST,       //  INPUT_RADIO_LIST  INPUT_DROPDOWN_LIST
                        'buttonsTemplate' => '{submit}',
                        'inlineSettings' => [
                            'closeButton' => '<button class="btn btn-sm btn-danger kv-editable-close"><i class="glyphicon glyphicon-remove"></i></button>',
                        ],
                        'placement' => 'left',
                        'options' => ['style' => 'height:60px'],
                    ];
                },
                'hAlign' => 'center',
                'vAlign' => 'middle',
                'width' => '10%'
            ],
            [
                'class' => 'kartik\grid\EditableColumn',
                'attribute' => 'sort',
                'editableOptions' => function ($model, $key, $index) {
                    return [
                        'model' => $model,
                        'attribute' => 'sort',
                        'options' => [
                            'id' => 'Config-' . "{$index}" . 'sort',
                            'name' => 'Config[' . "{$index}" . '][sort]',
                            'style' => 'width:auto;',
                            'title' => $model->sort,
                        ],
                        'formOptions' => ['action' => ['edit-table']],
                        'buttonsTemplate' => '{submit}',
                        'inlineSettings' => [
                            'closeButton' => '<button class="btn btn-sm btn-danger kv-editable-close"><i class="glyphicon glyphicon-remove"></i></button>',
                        ],
                        'asPopover' => true,
                        'placement' => 'right',
                    ];
                },
                'hAlign' => 'center',
                'vAlign' => 'middle',
                'width' => '5%',
            ],
            [
                'class' => 'kartik\grid\EditableColumn',
                'attribute' => 'status',
                'label' => '状态',
                'editableOptions' => function ($model, $key, $index) {
                    return [
                        'model' => $model,
                        'value' => $model->status,
                        'data' => Config::getStatus(),
                        'displayValueConfig' => Config::getStatus(),
                        'asPopover' => true,
                        'formOptions' => ['action' => ['edit-table']],
                        'inputType' => Editable::INPUT_RADIO_LIST,       //  INPUT_RADIO_LIST  INPUT_DROPDOWN_LIST
                        'buttonsTemplate' => '{submit}',
                        'inlineSettings' => [
                            'closeButton' => '<button class="btn btn-sm btn-danger kv-editable-close"><i class="glyphicon glyphicon-remove"></i></button>',
                        ],
                        'placement' => 'left',
                        'options' => ['style' => 'height:60px'],
                    ];
                },
                'hAlign' => 'center',
                'vAlign' => 'middle',
                'width' => '10%'
            ],
            [
                'class' => 'yii\grid\ActionColumn',
                'header' => '操作',
                'template' => '{update} {delete}',
                'options' => ['width' => '200px;'],
                'buttons' => [
                    'update' => function ($url, $model, $key) {
                        return Html::a('<i class="fa fa-edit"></i> 编辑', ['update','id'=>$key], [
                            'title' => Yii::t('app', '编辑'),
                            'class' => 'btn btn-xs green'
                        ]);
                    },
                    'delete'=>function($url,$model,$key)
                    {
                        $options=[
//                            'title'=>Yii::t('app', '删除'),
//                            'aria-label'=>Yii::t('app','删除'),
//                            'data-confirm'=>Yii::t('app','您确定要执行删除操作吗？'),
//                            'data-method'=>'post',
//                            'data-pjax'=>'0',
                            'id' => $key,
                            'class' => 'btn btn-xs red btn-delete'
                        ];
                        return Html::a('<i class="fa fa-times"></i> 删除',null,$options);
                    }
                ],
            ],
        ],
    ]); ?>
</div>

<script language="JavaScript">
    <?php $this->beginBlock(Yii::$app->controller->route) ?>

    $(document).ready(function () {

        // 编辑
        $(".btn-update").on("click", function () {
            var ids = $("#grid-id").yiiGridView("getSelectedRows");
            if (!ids.length) {
                layer.alert('请选中一条记录', {'title': '提示', 'icon': 7});
                return false;
            }

            window.location.href = '<?= Url::toRoute('update') ?>' + '?id=' + ids[0];
        });

        // 删除
        $(".btn-delete").on("click", function () {
            var id = $(this).attr('id');
            if (!id.length) {
                layer.alert('此记录一次，请刷新重试', {'title': '提示', 'icon': 7});
                return false;
            }

            // 询问框
            layer.confirm('您确定要执行删除操作吗？', {
                btn: ['确认', '取消'] //按钮
            }, function (index) {
                // 提交删除
                subDel(id);
            }, function (index) {

            });
        });

        // 批量删除
        $(".batch-delete").on("click", function () {
            var ids = $("#grid-id").yiiGridView("getSelectedRows");
            if (!ids.length) {
                layer.alert('请选择至少一条记录', {'title': '提示', 'icon': 7});
                return false;
            }

            // 询问框
            layer.confirm('您确定要执行删除操作吗？', {
                btn: ['确认', '取消'] //按钮
            }, function (index) {
                //提交删除
                subDel(ids);
            }, function (index) {

            });


        });

        /**
         * 提交删除
         * @param ids
         */
        function subDel(ids) {
            // 表单提交
            $.ajax({
                url: '<?= Url::toRoute('delete') ?>' + '?sync=true&id=' + ids,
                type: 'post',
                dataType: "json",
                data: {
                    _csrf: "<?= Yii::$app->request->csrfToken ?>"
                },
                beforeSend: function () {
                    layer.msg('删除中...');
                },
                success: function (r) {
                    console.log(r)
                    layer.close(layer.index);
                    if (r.status == 1) {
                        layer.msg(r.info, {icon: 1});
                        $.pjax.reload({container: "#grid-pjax"});
                    } else {
                        layer.alert(r.info, {'title': '提示', 'icon': 7});
                    }
                },
                error: function () {
                    alert('系统错误');
                    return false;
                }
            });
        }

    });

    jQuery(document).ready(function() {
        highlight_subnav('system/config/index'); //子导航高亮
    });

    <?php
    $this->endBlock();
    $this->registerJs($this->blocks[Yii::$app->controller->route], \yii\web\View::POS_END);
    ?>
</script>
<?php Pjax::end(); ?>
